package com.huiyeji.xlsx;

import org.apache.poi.ss.usermodel.*;

public class c {

    private Workbook wk;

    private Row r;

    private int p;

    public c(Workbook wk, Row r, int iPos) {
        this.wk = wk;
        this.r = r;
        this.p = iPos;
    }

    /**
     * 写入单元格并指向后移
     * @param ct
     * @param bci
     */
    public void writeBack(String ct,Short bci){
        Cell cell = r.createCell(p);
        CellStyle cellStyle = wk.createCellStyle();
        cellStyle.setFillForegroundColor(bci);
        cellStyle.setFillPattern((short) 1);
        cellStyle.setBorderBottom((short) 1);
        cellStyle.setBorderLeft((short) 1);
        cellStyle.setBorderRight((short) 1);
        cellStyle.setBorderTop((short) 1);
        cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
        cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
        cell.setCellStyle(cellStyle);
        cell.setCellType(1);
        cell.setCellValue(ct);
        back();
    }

    public void writeSuccessBack(String content){
        writeBack(content,IndexedColors.GREEN.getIndex());
    }

    public void writeWarnBack(String content){
        writeBack(content,IndexedColors.YELLOW.getIndex());
    }

    /**
     * 向某个单元格写入数据(info)(不影响指向)
     * @param content
     */
    public void writeInfoBack(String content){
        writeBack(content,IndexedColors.WHITE.getIndex());
    }

    public void writeFailedBack(String content){
        writeBack(content,IndexedColors.RED.getIndex());
    }

    /**
     * 向某个单元格写入数据(success:绿色)(不影响指向)
     * @param tempPos
     * @param content
     */
    public void writeSuccess(int tempPos,String content){
        write(tempPos,content,IndexedColors.GREEN.getIndex());
    }

    /**
     * 向某个单元格写入数据(failed:红色)(不影响指向)
     * @param tempPos
     * @param content
     */
    public void writeFailed(int tempPos,String content){
        write(tempPos,content,IndexedColors.RED.getIndex());
    }

    /**
     * 向某个单元格写入数据(info:白色)(不影响指向)
     * @param tempPos
     * @param content
     */
    public void writeInfo(int tempPos,String content){
        write(tempPos,content,IndexedColors.WHITE.getIndex());
    }

    /**
     * 向某个单元格写入数据(warn:黄色)(不影响指向)
     * @param tempPos
     * @param content
     */
    public void writeWarn(int tempPos,String content){
        write(tempPos,content,IndexedColors.YELLOW.getIndex());
    }

    /**
     * 临时写入某个单元格(不影响当前指向)
     * @param tempPos 单元格index
     * @param content 内容
     * @param backgroundColorIndex 填充颜色
     */
    public void write(int tempPos,String content,Short backgroundColorIndex){
        CellStyle cellStyle = wk.createCellStyle();
        cellStyle.setFillForegroundColor(backgroundColorIndex);
        cellStyle.setFillPattern((short) 1);
        cellStyle.setBorderBottom((short) 1);
        cellStyle.setBorderLeft((short) 1);
        cellStyle.setBorderRight((short) 1);
        cellStyle.setBorderTop((short) 1);
        cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
        cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
        Cell cell = r.createCell(tempPos);
        cell.setCellStyle(cellStyle);
        cell.setCellType(1);
        cell.setCellValue(content);
    }

    /**
     * 单元格指向后移
     */
    private void back(){
        p++;
    }

    /**
     * 单元格指向前移
     */
    private void front(){
        p--;
    }

    public int getP(){
        return p;
    }
}
